package com.gopay.checkorder.service.task.thread;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.manager.BankOrderManager;
import com.gopay.checkorder.service.BcdlOrderSendBankService;
import com.gopay.checkorder.service.sentinel.BcdlOrderSentinel;
import com.gopay.checkorder.service.task.thread.batch.BcdlResultDeal4BatchThread;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BcdlResult;
import com.gopay.common.domain.bank.bcdl.BcdlPacketRes;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;
import com.gopay.common.util.spring.SpringContextUtil;
import com.gopay.gass.service.GassPayRemoteService;

/**
 * 工行银企批量付款多线程任务
 *
 * @author lynn
 * @data
 */
public class IcbcBcdlOrderBatchSendMTTaskThread implements Runnable {

	private Logger logger = LoggerFactory.getLogger(IcbcBcdlOrderBatchSendMTTaskThread.class);

	private BcdlOrderSentinel sentinel;

	@Resource(name = "bankOrderManager")
	protected BankOrderManager bankOrderManager;
	
	private String bank;

	private CountDownLatch countDownLatch;

	private List<BankOrder> order;
	
	private String packetNo;

	private String label;
	
	private void printLog(StopWatch sw, String bank, String prefix) {
		sw.split();
		logger.error(prefix + "------,bank=" + bank+  ",耗时="
				+ sw.getSplitTime());
	}
	

	public IcbcBcdlOrderBatchSendMTTaskThread(String label, String bank, List<BankOrder> order,String packetNo, CountDownLatch countDownLatch, BcdlOrderSentinel sentinel, BankOrderManager bankOrderManager) {
		this.label = label;
		this.bank = bank;
		this.order = order;
		this.packetNo= packetNo;
		this.countDownLatch = countDownLatch;
		this.sentinel = sentinel;
		this.bankOrderManager = bankOrderManager;
	}

	@Override
	public void run() {

		BcdlResult result = null;
		BcdlPacketRes res = new BcdlPacketRes();
		StopWatch sw = new StopWatch();
		sw.start();
		printLog(sw, bank,"IcbcBcdlOrderBatchSendMTTaskThread------start,packetNo="+packetNo);
		if (sentinel.isFuse()) {
			logger.error(bank + "多线程付款熔断，当前订单不做处理，packetNo="+packetNo);
			countDownLatch.countDown();
			return;
		}
		
		// 日切判断
		if (!bankOrderManager.isLegalTime(bank)) {
			return;
		}

		try {
			logger.error("IcbcBcdlOrderBatchSendMTJob--插入发往银行日志--开始");
			bankOrderManager.saveCheckBcdlSendBankLogByOrders(order);
			logger.error("IcbcBcdlOrderBatchSendMTJob--插入发往银行日志--结束");

		} catch (Exception e) {
			logger.error("IcbcBcdlOrderBatchSendMTJob--插入发往银行日志--异常", e);
			sentinel.stop(3);
			return;
		}

		try {
			String beanName = getSendBankBeanName(bank);

			if (false == SpringContextUtil.containsBean(beanName)) {
				logger.info("spring bean does not exist: " + beanName);
				countDownLatch.countDown();
				return;
			}

			BcdlOrderSendBankService sendBankService = (BcdlOrderSendBankService) SpringContextUtil.getBean(beanName);

			logger.info("线程池中处理数据开始packetNo=" + packetNo);

			try {
				logger.error("银企直连 发送银行--开始 ，packetNo=" + packetNo + ",bank=" + bank);
				result = sendBankService.send4batch(order, packetNo, label, bank);
				logger.error( "银企直连 发送银行--结束 ，packetNo=" + packetNo + ",bank=" + bank + ",resultCode=" + result.getResultCode());


			} catch (Exception e) {
				logger.error("多线程任务发送银行异常 ，packetNo=" + packetNo + ",bank=" + bank, e);
			} finally {
				logger.error("--------调用熔断器开始---------");
				sentinel.batchPatrol(packetNo, result, res);
				logger.error("--------调用熔断器结束---------");
			}

		} catch (GopayException e) {
			logger.error("调用bis异常packetNo=" + packetNo, e);

		} catch (Exception e) {
			logger.error("调用bis异常packetNo=" + packetNo, e);

		} finally {
			countDownLatch.countDown();
			printLog(sw, bank,"IcbcBcdlOrderBatchSendMTTaskThread------end,packetNo=" + packetNo);
			sw.stop();
		}
	}
	


	private String getSendBankBeanName(String bankCode) throws GopayException {
		String beanName = bankCode + "-BCDL";
		return beanName;
	}

	public BankOrderManager getBankOrderManager() {
		return bankOrderManager;
	}

	public void setBankOrderManager(BankOrderManager bankOrderManager) {
		this.bankOrderManager = bankOrderManager;
	}


}
